package com.jzo2o.foundations.job;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.zhao
 * @Date 2025/7/3 12:15
 */
@Component
public class SpringCacheSyncHandler {
	@Autowired
	private IRegionService regionService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private HomeService homeService;

	@XxlJob(value = "activeRegionCacheSync")
	public void activeRegionCacheSync(){
		String regionRedisKey = RedisConstants.CacheName.JZ_CACHE + "::" + "ACTIVE_REGIONS";

		//一、定时刷新区域列表缓存
		//1. 删除旧的通区域列表缓存
		redisTemplate.unlink(regionRedisKey);

		//2. 添加新的通区域列表缓存
		regionService.queryActiveRegionListCache();
		List<RegionSimpleResDTO> regionList = regionService.queryActiveRegionListCache();

		//二、定时刷新区域下的首页服务列表缓存
		regionList.forEach(x->{
			Long regionId = x.getId();

			//1. 删除旧的服务列表缓存
			String serveIconRedisKey = RedisConstants.CacheName.SERVE_ICON + "::" + regionId;
			redisTemplate.unlink(serveIconRedisKey);

			//2. 添加新的服务列表缓存
			homeService.firstPageServeList(regionId);
		});

	}
}
